import os
import time
from hashlib import md5

import requests
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

browser = webdriver.Chrome()
wait = WebDriverWait(browser, 10)
workspace = os.path.expanduser('~') + "/mockAny/python/"

def fetch_jrtt_page(keyword):
  browser.get('https://www.toutiao.com/')
  # 等待查询Input
  input = wait.until(
    EC.presence_of_element_located(
      (By.CSS_SELECTOR, '#root > div > div.search-container > div > div.search > input[type=text]'))
  )
  # 发送关键字
  input.send_keys(keyword)
  # 等待查询按钮
  search_btn = input = wait.until(
    EC.presence_of_element_located((By.CSS_SELECTOR, '#root > div > div.search-container > div > div.search > button'))
  )
  # 回车
  search_btn.send_keys(Keys.ENTER)
  time.sleep(3)
  # 切换到最新的window窗口
  browser.switch_to.window(browser.window_handles[-1])

  # 查找页面内的资讯tab
  tabsWrapper = browser.find_element(By.CSS_SELECTOR, '.cs-tabs-bar-wrap')
  aZixun = tabsWrapper.find_element(By.CSS_SELECTOR, "a.cs-tabs-item:nth-child(2)")
  # 点击资讯tab按钮
  aZixun.click()
  time.sleep(1)

  ## 分页 获取10页内容
  for i in range(10):
    # 获取内容
    fetch_content()
    # 当前页内容拉取完成后，点击下一页
    page_a_wrapper = wait.until(
      EC.presence_of_element_located((By.CSS_SELECTOR, '.cs-view.cs-view-block.cs-pagination'))
    )
    page_a_s = browser.find_elements(By.CSS_SELECTOR, '.cs-view.cs-view-block.cs-pagination a')
    a_next_ele = page_a_s[len(page_a_s) - 1]
    a_next_ele.click()
    time.sleep(3)


# 获取页面内容
def fetch_content():
  # 等待资讯内容刷新
  result_container = wait.until(
    EC.presence_of_element_located((By.CSS_SELECTOR, '.s-result-list'))
  )
  # 拿到keyword信息相关的数组
  result_content_s = result_container.find_elements(By.CSS_SELECTOR, '.result-content')
  # 遍历咨询信息a标签
  for ele in result_content_s:
    aEle = ele.find_element(By.CSS_SELECTOR, 'a')
    # 点击进入咨询详情页
    aEle.click()
    # 切换到详情页window窗口
    browser.switch_to.window(browser.window_handles[-1])
    # 拿到图片元素
    imgElems = browser.find_elements(By.CSS_SELECTOR, '.pgc-img img')
    for item in imgElems:
      imgItem = item
      # 下载图片
      download_img(imgItem)
    # 关闭当前window页
    browser.close()
    # 切换回window[1]，也就是keyword资讯列表页
    browser.switch_to.window(browser.window_handles[1])

def download_img(imgElem):
  try:
    src = imgElem.get_attribute('src')
    srcTmp = src.split('?')[0]
    # base_name = os.path.basename(srcTmp)
    headers = {
      'Referer': 'https://www.toutiao.com/',
      'sec-ch-ua': '" Not;A Brand";v="99", "Google Chrome";v="97", "Chromium";v="97"',
      'sec-ch-ua-mobile': '?0',
      'sec-ch-ua-platform': 'Windows',
      'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36'
    }
    resp = requests.get(src, headers)
    saveImage(resp.content)
  except Exception:
    print(Exception)


def saveImage(content):
  filePath = '{0}/{1}.{2}'.format(workspace, md5(content).hexdigest(), 'jpg')
  with open(filePath, 'wb') as f:
    f.write(content)
    f.close()

if __name__ == '__main__':
  if not os.path.exists(workspace):
    os.makedirs(workspace)
  fetch_jrtt_page('街拍')
